package view 
{
	import com.greensock.TweenMax;
	import controller.GameController;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import model.GameData;
	import sound.SoundManager;
	/**
	 * ...
	 * @author bimkute
	 */
	public class BackGround extends Base 
	{
		private var _arrSound:Array = [];
		
		//**********************
		
		private var _bg:MovieClip;
		private var _boatMen:MovieClip;
		private var arrrayBG:Array = ["Morning", "Afternoon", "Night"];
		
		private var rd1:int;
		private var _timeMove:int;
		
		
		public function BackGround() 
		{	
			rd1 = int(Math.random() * arrrayBG.length);
			//var rd2:int = int(Math.random() * arrrayBG.length);
			
			// add sound
			_arrSound = ["Bg1", "Bg2", "Bg3"]
			if (GameData.getInstance().checkSoundOn) 
			{
				SoundManager.getInstance().playSound(_arrSound[rd1], 1000);
			}
			
			
			
			//_bg = GameController.getInstance().getClass(arrrayBG[rd1], GameData.getInstance().arrLoad[1][1]);
			_bg = GameController.getInstance().getClass(arrrayBG[0], GameData.getInstance().arrLoad[1][1]);
			addChild(_bg);
			
			_boatMen = GameController.getInstance().getClass("Boatmen", GameData.getInstance().arrLoad[1][1]);
			addChild(_boatMen);
			_boatMen.width = _boatMen.width / 12;
			_boatMen.height = _boatMen.height / 12;
			
			//_boatMen.x = Math.floor(Math.random() * 400 + 400);
			_boatMen.x = 550;
			//_boatMen.x = 750
			_boatMen.y = 230;
			trace("GameData.getInstance().timeRemain di chuyen", GameData.getInstance().timeRemain)
			//if (_boatMen.x > 650)
			//{
				//_boatMen.scaleX = -1 / 12;
				//TweenMax.to(_boatMen, 140, {bezier:[{x:600, y:320}], scaleX:-1/6, scaleY:1/6, onComplete:FinishMove});
			//}
			//else
			//{
				//TweenMax.to(_boatMen, 140, {bezier:[{x:600, y:320}], scaleX:1/6, scaleY:1/6, onComplete:FinishMove});
			//}
				
			GameData.getInstance().addEventListener("soundOn", onSoundOn);
			GameData.getInstance().addEventListener("soundOff", onSoundOff);
		}
		
		public function moveThuyen(timer:int):void 
		{
			_timeMove = timer - 50;
			trace("move", _timeMove)
			if (_boatMen.x > 650)
			{
				//_boatMen.scaleX = -1 / 12;
				TweenMax.to(_boatMen, _timeMove, {bezier:[{x:650, y:320}], scaleX:-1/6, scaleY:1/6, onComplete:FinishMove});
			}
			else
			{
				TweenMax.to(_boatMen, _timeMove, {bezier:[{x:650, y:320}], scaleX:1/6, scaleY:1/6, onComplete:FinishMove});
			}
		}
		
		private function onSoundOff(e:Event):void 
		{
			stopSoundBg();
		}
		
		private function onSoundOn(e:Event):void 
		{
			rd1 = int(Math.random() * arrrayBG.length);
			SoundManager.getInstance().playSound(_arrSound[rd1], 1000);
		}
		
		public function stopSoundBg():void
		{
			SoundManager.getInstance().stopSound(_arrSound[rd1]);
		}
		//private function moveBoat(e:Event):void 
		//{
			
			//if (GameData.getInstance().timeRemain > 0)
			//{
				//if (_boatMen.x > 650)
				//{
					//_boatMen.scaleX = -1 / 12;
					//TweenMax.to(_boatMen, 140, {bezier:[{x:600, y:320}], scaleX:-1/6, scaleY:1/6, onComplete:FinishMove});
				//}
				//else
				//{
					//TweenMax.to(_boatMen, 140, {bezier:[{x:600, y:320}], scaleX:1/6, scaleY:1/6, onComplete:FinishMove});
				//}
			//}
			//
		//}
		
		public function visibleBoatmen():void
		{
			if (_boatMen)
			{
				_boatMen.visible = false;
			}
		}
		private function FinishMove():void 
		{
			TweenMax.killAll();
			//if (GameData.getInstance().timeRemain > 0)
			//{
				//_boatMen.x = 600;
				//_boatMen.y = 320;
				//_boatMen.scaleX = _boatMen.scaleY = 1/6
			//}
			trace("FinishMove")
		}
		
		public function removeAllEvent():void
		{
			//GameData.getInstance().removeEventListener("timeRemain", moveBoat);
			if (_boatMen)
			{
				removeChild(_boatMen);
				_boatMen = null;
			}
			if (_bg)
			{
				removeChild(_bg);
				_bg = null;
			}
			stopSoundBg();
			GameData.getInstance().removeEventListener("soundOn", onSoundOn);
			GameData.getInstance().removeEventListener("soundOff", onSoundOff);
			TweenMax.killAll();
		}
		
	}

}